#include "Door.h"
#include "../SGD Wrappers/SGD_Event.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "GameplayState.h"


Door::Door() : Listener(this)
{

	RegisterForEvent("OPEN");
	RegisterForEvent("ALARM_TRIGGER");
	RegisterForEvent("ALARM_OFF");




}


Door::~Door()
{

}

/*virtual*/ void Door::Update(float dt) /*override*/
{
	if (m_bIsOpen == true )
	{
		if (door.right > door.left)
		{
			door.right -= 100 * dt;

			if (door.right < door.left)
				door.right = door.left;
		}
	}
	else
	{
		if (door.right < door.left + 200)
		{
			door.right += 100 * dt;

			if (door.right > door.left + 200)
				door.right = door.left + 200;
		}
	}
}
/*virtual*/ void Door::Render() /*override*/
{
	SGD::GraphicsManager* pGraphics = SGD::GraphicsManager::GetInstance();
	SGD::Point tempPos = { m_ptPosition.x - GameplayState::GetInstance()->GetCamera()->GetPosition().x, m_ptPosition.y - GameplayState::GetInstance()->GetCamera()->GetPosition().y };

	SGD::Rectangle drawRect = door;
	drawRect.Offset(-GameplayState::GetInstance()->GetCamera()->GetPosition().x, -GameplayState::GetInstance()->GetCamera()->GetPosition().y);
	pGraphics->DrawRectangle(drawRect, SGD::Color(255, 0, 0, 0));
	SGD::GraphicsManager::GetInstance()->DrawRectangle(GetRect(), SGD::Color(0, 0, 0, 0), SGD::Color(255, 255, 0, 0));


}
/*virtual*/ void Door::HandleCollision(const IBase* pOther) /*override*/
{

}
/*virtual*/ void Door::HandleEvent(const SGD::Event* pEvent)
{
	
	 if (pEvent->GetEventID() == "OPEN")
	{
		 m_bIsLocked = false;
		 m_bIsOpen = true;

	}
	else if (pEvent->GetEventID() == "ALARM_TRIGGER")
	{
		m_bIsOpen = false;

	}
	else if (pEvent->GetEventID() == "ALARM_OFF")
	{
		if (m_bIsLocked == false)
			m_bIsOpen = true;

	}
	else if (pEvent->GetEventID() == keyPadEvent.c_str())
	{
		m_bIsLocked = false;
		m_bIsOpen = true;

	}
}

/*virtual*/ SGD::Rectangle	Door::GetRect(void)	const
{
	
	SGD::Rectangle collisionRect = door;

	SGD::Point renderPos = SGD::Point(m_ptPosition.x

		- GameplayState::GetInstance()->GetCamera()->GetPosition().x,
		m_ptPosition.y

		- GameplayState::GetInstance()->GetCamera()->GetPosition().y);

	return SGD::Rectangle(renderPos, SGD::Size(collisionRect.right - collisionRect.left, collisionRect.bottom - collisionRect.top));
}